package com.taobao.ad.hadoop.main;
import java.util.*; 
import com.taobao.ad.hadoop.common.JobNode; 
public class InitJob {
	public static List<JobNode> GenerageJobPlan(
			List<Map<String, String>> jobconfList, String input, String output,
			String intermediate) throws Exception {
		List<JobNode> jobNodeList = new ArrayList<JobNode>();
		Map<String, JobNode> jobnodeMap = new HashMap<String, JobNode>();
		for (int i = 0; i < jobconfList.size(); i++) {
			Map<String, String> kvMap = jobconfList.get(i);
			String className = kvMap.get("class");
			JobNode node = (JobNode) (Class.forName(className).newInstance());
			for (String key : kvMap.keySet()) {
				String value = kvMap.get(key);
				node.set(key, value);
			}
			String dependence = node.get("dependence");
			if (dependence != "") {
				String[] dependences = dependence.split(",");
				String inputPath = "";
				for (int j = 0; j < dependences.length; j++) {
					String dependenceJobName = dependences[j];
					if (j > 0) {
						inputPath += ",";
					}
					inputPath += jobnodeMap.get(dependenceJobName).get(
							"outputPath");
				}
				node.set("inputPath", inputPath);
			}
			node.set("outputPath", intermediate + "/" + node.get("jobname"));
			if (i == 0) {
				node.set("inputPath", input);
			}
			if (i == jobconfList.size() - 1) {
				node.set("outputPath", output);
			}
			node.ConfigJob();
			jobnodeMap.put(node.get("jobname"), node);
			jobNodeList.add(node);
		}
		return jobNodeList;
	}
}
